﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecreate.Business;
using System.Text.RegularExpressions;

namespace Ecreate.Web.systemanage.order
{
    public partial class orderlist : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string type = Request.QueryString["type"];
                HyperLinkField col = new HyperLinkField();
                col.Text = "查看单据";
                if (type == "5")
                {
                    //<asp:HyperLinkField Text="查看单据" DataNavigateUrlFields="OrderID" DataNavigateUrlFormatString="./ordercrud.aspx?id={0}" />
                    col.DataNavigateUrlFields = new string[] { "ID" };
                    col.DataNavigateUrlFormatString = "./ordercrud.aspx?type=i&id={0}";
                }
                else
                {
                    col.DataNavigateUrlFields = new string[] { "OrderID" };
                    col.DataNavigateUrlFormatString = "./ordercrud.aspx?type=o&id={0}";
                }
                gv.Columns.Add(col);
                if (type == "1")//byid
                {
                    lblID.Value = "循环滚动器序号：";
                    ddlClass1.Visible = false;
                }
                else if (type == "2")//bysellphonenumber
                {
                    lblID.Value = "销售商手机号码：";
                    ddlClass1.Visible = false;
                }
                else if (type == "3" || type == "4" || type == "5")//currentdayorderbyid||currentdayorderbytimes||SelectAllUnCheckedOrder
                {
                    lblID.Visible = false;
                    txtID.Visible = false;
                    ddlClass1.Visible = false;
                    btnQuery.Visible = false;
                    gv.Visible = true;
                    BindData();
                }
                else if (type == "6")//bysellphonenumber
                {
                    lblID.Value = "产品大类：";
                    txtID.Visible = false;
                    ddlClass1.Visible = true;
                    BindDDLProductClass(ddlClass1, "-1");
                }
            }
        }

        private void BindDDLProductClass(DropDownList ddl, string parentclassid)
        {
            ProductClassService service = new ProductClassService();
            BindDDL(ddl, service.getProductClassByParentID(parentclassid), "classname", "classid");
        }

        private void BindDDL(DropDownList ddl, IList list, string TextField, string ValueField)
        {
            ddl.DataSource = list;
            ddl.DataTextField = TextField;
            ddl.DataValueField = ValueField;
            ddl.DataBind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            gv.Visible = true;
            BindData();
        }

        private void BindData()
        {
            IList list = null;
            OrderService service = new OrderService();
            if (txtID.Text == "" && txtID.Visible)
            {
                list = service.SelectAll();
            }
            else
            {
                string type = Request.QueryString["type"];
                if (type == "1")
                {
                    string orderid = txtID.Text;
                    list = service.SelectByID(orderid);
                }
                else if (type == "2")
                {
                    string phonenumber = txtID.Text;
                    list = service.SelectBySellPhoneNumber(phonenumber);
                }
                else if (type == "3")
                {
                    list = service.SelectCurrentDayOrderByID();
                }
                else if (type == "4")
                {
                    list = service.SelectCurrentDayOrderByTimes();
                }
                else if (type == "5")
                {
                    list = service.SelectAllUnCheckedOrder();
                }
                else if (type == "6")
                {
                    string class1 = ddlClass1.SelectedValue;
                    list = service.SelectByProductClass1(class1);
                }
            }
            gv.DataSource = list;
            gv.DataBind();
        }

        protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                foreach (TableCell cell in e.Row.Cells)
                {
                    string text = cell.Text;
                    cell.Text = GetHeaderText(text);
                }
            }
        }

        private string GetHeaderText(string text)
        {
            string headertext = text;
            string[] fields = "OrderID,KickOrderID,SN,Times,SellAddress,SellPhoneNumber,ProductName,Price,CustName,CustPhoneNumber,CustAddress,PayAccount,PayDate,IsCheckName,CheckDate".Split(new char[] { ',' });
            string[] titles = "订单编号,被此订单顶掉,当前排序,被顶掉次数,销售地址,销售电话,产品名称,价格,消费者姓名,消费者电话,消费者收件地址,付款账号,付款日期,付款确认,确认日期".Split(new char[] { ',' });
            string type = Request.QueryString["type"];
            if (type == "5")
            {
                fields = "ID,KickOrderID,SN,Times,SellAddress,SellPhoneNumber,ProductName,Price,CustName,CustPhoneNumber,CustAddress,PayAccount,PayDate,IsCheckName,CheckDate".Split(new char[] { ',' });
            }
            for (int i = 0; i < fields.Length; i++)
            {
                if (fields[i] == text)
                {
                    headertext = titles[i];
                    break;
                }
            }
            return headertext;
        }

        protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gv.PageIndex = e.NewPageIndex;
            BindData();
        }
    }
}
